<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  admin_activity.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-19.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Admin_goods extends CI_Controller {

	 function __construct()
	 {
		parent::__construct();
 		$this->load->model('common/security_model');
 		$this->load->model('admin/admin_activity_model');
 		$this->load->model('activity_model');
 		$this->load->model('goods_model');
		if($this->security_model->isAdminLogin() === FALSE){
          showmessage('请登录', 'admin/login/index', 3);
		}
		$this->brands = $this->config->item('brand');
		
	 }

	function index($aid)
	{
		$aid = $aid ? intval($aid) : '';
		if (!$aid) {
			showmessage('非法来源');
		}
		$data['goods'] = $this->goods_model->get_goods($aid);
		$data['activity'] = $this->activity_model->get_activity_by_aid($aid);
		$data['aid'] = $aid;
		$data['brands'] = $this->brands;
		// 分类缓存
		$data['category'] = $this->config->item('category');
		$this->load->template('admin/goods_tpl', $data);
	}
	function add_goods($aid){
		$aid = $aid ? intval($aid) : '';
		if (!$aid) {
			showmessage('非法来源');
		}
		$set = $this->input->post('set');
		$thevaluesubmit = $this->input->post('thevaluesubmit') ? $this->input->post('thevaluesubmit') : '';
		if ($thevaluesubmit) {
			//此处需要验证表单
			$insertdata = array(
					'aid' => $aid,
					'name' => $set['name'],
					'imageurl' => $set['imageurl'],
					'url' => $set['url'],
					'price' => $set['price'],
					'marketprice' => $set['marketprice'],
					'discount' => $set['discount'],
					'brandid' => $set['brandid'],
					'catid' => $set['catid'],
					'img_mode' => $set['img_mode'],
					'dateline' => now(),
					'updatetime' => now(),
				);
			$gid = $this->goods_model->insert_goods($insertdata);
			if ($set['img_mode'] == 1) {
				// 上传文件
				if ($_FILES['userfile']['name']) {
					$config = array();
					$config['file_name'] = 'goods_'.$gid.'_'.date('Ymd',now());
					$result = upload_file($config);
					if ($result['error']) {
						showmessage('上传失败','admin/admin_goods/index/'.$aid, 3);				
					}else{
						$updatedata = array(
								'gid' => $gid,
								'uploadimg' => $result['upload_data']['basename'].'/'.$result['upload_data']['file_name'],
							);
						$result = $this->goods_model->update_goods($updatedata);
					}
				}
			}
			if ($gid) {
				showmessage('添加成功','admin/admin_goods/index/'.$aid, 3);
			}else{
				showmessage('添加失败','admin/admin_goods/index/'.$aid, 3);				
			}
		}
		$data['aid'] = $aid;
		//品牌缓存
		$data['brands'] = $this->brands;
		// 分类缓存
		$data['category'] = $this->config->item('category');
		$this->load->template('admin/add_goods_tpl', $data);				
	}

	function edit_goods($gid){
		$set = $this->input->post('set');
		$thevaluesubmit = $this->input->post('thevaluesubmit') ? $this->input->post('thevaluesubmit') : '';
		$gid = $gid ? intval($gid) : 0;
		if (empty($gid)) {
			showmessage('非法来源');
		}
		$goods = $this->goods_model->get_goods_by_gid($gid);
		$goods['uploadimg'] = base_url().'upload/attachments/'.$goods['uploadimg'];
		//表单提交
		if (!empty($thevaluesubmit) && !empty($gid)) {
			//此处需要验证表单
			$updatedata = array(
					'gid' => $gid,
					'name' => $set['name'],
					'imageurl' => $set['imageurl'],
					'url' => $set['url'],
					'price' => $set['price'],
					'marketprice' => $set['marketprice'],
					'discount' => $set['discount'],
					'brandid' => $set['brandid'],
					'catid' => $set['catid'],
					'img_mode' => $set['img_mode'],
					'updatetime' => now(),
				);
			$id = $this->goods_model->update_goods($updatedata);
			if ($set['img_mode'] == 1) {
				// 上传文件
				if ($_FILES['userfile']['name']) {
					$config = array();
					$config['file_name'] = 'goods_'.$gid.'_'.date('Ymd',now());
					$result = upload_file($config);
					if ($result['error']) {
						showmessage('上传失败','admin/admin_goods/index/'.$goods['aid'], 3);				
					}else{
						$updatedata = array(
								'gid' => $gid,
								'uploadimg' => $result['upload_data']['basename'].'/'.$result['upload_data']['file_name'],
							);
						$result = $this->goods_model->update_goods($updatedata);
					}
				}
			}
			showmessage('编辑成功','admin/admin_goods/index/'.$goods['aid'], 3);
			
		}
		$data['gid'] = $gid;
		$data['thevalue'] = $goods;
		//品牌缓存
		$data['brands'] = $this->brands;
		// 分类缓存
		$data['category'] = $this->config->item('category');
		$this->load->template('admin/edit_goods_tpl', $data);	
	}
	
	//单个删除 controller
    function delete($gid, $aid)
    {
		if (empty($gid)) {
			showmessage('没有选择任何商品');
		}
		
		$ids = array($gid);
		if (empty($ids)) {
			showmessage('没有选择任何商品');
		}
		$result = $this->goods_model->deletes_goods($ids);
		if ($result) {
			showmessage('删除成功','admin/admin_goods/index/'.$aid, 3);	
		}else{
			showmessage('删除失败','admin/admin_goods/index/'.$aid, 3);
		}
    }

	//批量删除 controller
	function deletes()
	{	
		$ids = $this->input->post('ids');
		$aid = $this->input->post('aid') ? $this->input->post('aid') : '';
		if (empty($aid)) {
			showmessage('没有选择任何活动');
		}
			
		if (empty($ids)) {
			showmessage('没有选择任何商品');
		}
		$result = $this->goods_model->deletes_goods($ids);
		if ($result) {
			showmessage('删除成功','admin/admin_goods/index/'.$aid, 3);	
		}else{
			showmessage('删除失败','admin/admin_goods/index/'.$aid, 3);
		}
	}
}
?>